package com.hotacorp.opencloud.userservice.controller;

import java.util.Arrays;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.hotacorp.opencloud.common.commdata.Result;
import com.hotacorp.opencloud.common.mybatisplus.PageData;
import com.hotacorp.opencloud.userservice.entity.SysTenantEntity;
import com.hotacorp.opencloud.userservice.service.SysTenantService;

import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 租户信息表
 *
 * @author lwg
 * @email lwg@gmail.com
 * @date 2019-12-25 11:52:19
 */
@RestController
public class SysTenantController extends AbstractController {
    @Autowired
    private SysTenantService sysTenantService;

    /**
     * 列表
     */
    @RequestMapping(value = "/systenant/list", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:systenant:list')")
    public Result<?> list(@RequestParam Map<String, Object> params){
        PageData page = sysTenantService.queryPage(params);

        return Result.succeed("succeed", page);
    }


    /**
     * 信息
     */
    @RequestMapping(value = "/systenant/info/{tenantid}", method = {RequestMethod.OPTIONS, RequestMethod.GET, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:systenant:info')")
    public Result<?> info(@PathVariable("tenantid") Long tenantid){
		SysTenantEntity sysTenant = sysTenantService.getById(tenantid);

        return Result.succeed("succeed", sysTenant);
    }

    /**
     * 保存
     */
    @RequestMapping(value = "/systenant/save", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:systenant:save')")
    public Result<?> save(@RequestBody SysTenantEntity sysTenant){
		sysTenantService.save(sysTenant);

        return Result.succeed();
    }

    /**
     * 修改
     */
    @RequestMapping(value = "/systenant/update", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:systenant:update')")
    public Result<?> update(@RequestBody SysTenantEntity sysTenant){
		sysTenantService.updateById(sysTenant);

        return Result.succeed();
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/systenant/delete", method = {RequestMethod.OPTIONS, RequestMethod.POST})
    @PreAuthorize("hasAuthority('userservice:systenant:delete')")
    public Result<?> delete(@RequestBody Long[] tenantids){
		sysTenantService.removeByIds(Arrays.asList(tenantids));

        return Result.succeed();
    }

    @GetMapping(value = "/anon/tenantid", params = "domain")
    @ApiOperation(value = "根据域名查询租户")
    @ApiIgnore
    public Result<Long> findByMobile(@RequestParam("domain") String domain) {
    	Long ddd = sysTenantService.getTenantId(domain);
    	if (ddd == null) {
    		return Result.failed("没有找到域名");
    	} else {
        	return Result.succeed("succeed", ddd);
		}
    }

}
